#生成金额
from decimal import Decimal

import openpyxl


from entity.amount_entity import AmountEntity
from ut.set_save_path_config import get_sheet_address
from ut.windows_ui import show_warning


def  print_money_table(shipping_table_list,compute_table_list,date):

    file_name=date+"丸一出货金额表.xlsx"
    file_path=get_sheet_address()+"\\"+file_name
    wb=openpyxl.Workbook()
    ws=wb.create_sheet("金额表")
    wb.remove(wb["Sheet"])

    amount_list=[]
    compute_price_map={}
    for i in range(len(compute_table_list)):
        compute_price_map[compute_table_list[i].v_name]=compute_table_list[i].v_price
    for i in range(len(shipping_table_list)):
        data=AmountEntity(None,None,None,None,None)
        data.po=shipping_table_list[i].po
        data.p_name=shipping_table_list[i].name
        data.p_pcs_sum=shipping_table_list[i].quantity
        data.p_money=(
                Decimal(shipping_table_list[i].quantity)
                *Decimal(compute_price_map[shipping_table_list[i].name]))
        amount_list.append(data)
    print("amount")
    ws.cell(row=1,column=1).value="PO号"
    ws.cell(row=1,column=2).value="产品名称"
    ws.cell(row=1,column=3).value="产品数量"
    ws.cell(row=1,column=4).value="金额"

    for i in range(len(amount_list)):
        print(str(amount_list[i].po)+" "+str(amount_list[i].p_name)+" "+str(amount_list[i].p_pcs_sum)+" "+str(amount_list[i].p_money))
        ws.cell(row=i+2,column=1).value=amount_list[i].po
        ws.cell(row=i+2,column=2).value=amount_list[i].p_name
        ws.cell(row=i+2,column=3).value=amount_list[i].p_pcs_sum
        ws.cell(row=i+2,column=4).value=amount_list[i].p_money

    ws.cell(row=len(amount_list) + 2, column=3).value = '总金额：'
    ws.cell(row=len(amount_list)+2,column=4).value=sum([amount_list[i].p_money for i in range(len(amount_list))])
    #设置第四列的宽度
    ws.column_dimensions['D'].width = 15
    #设置第四列的格式为保留两位小数
    for cell in ws['D']:
        cell.number_format = '0.00'
    try:
        wb.save(file_path)
        show_warning("金额表已保存至："+ file_path)
    except Exception as e:
        show_warning("金额表保存失败，请检查金额文件是否被占用。")


